home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Gallery / Source / ManagerWindow.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-15  |  13.8 KB  |  592 lines

  1. #include <stream.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <stdio.h>
  6.  
  7. #include <clib/dos_protos.h>
  8. #include <clib/utility_protos.h>
  9.  
  10. #include "GUICINCLUDE:GUIC_FileExamine.hpp"
  11. #include "GUICINCLUDE:GUIC_DirectoryExamine.hpp"
  12. #include "GUICINCLUDE:GUIC_System.hpp"
  13. #include "GUICINCLUDE:GUIC_StringType.hpp"
  14. #include "GUICINCLUDE:GUIC_Screen.hpp"
  15. #include "GUICINCLUDE:GUIC_Application.hpp"
  16. #include "GUICINCLUDE:GUIC_Listview.hpp"
  17. #include "GUICINCLUDE:GUIC_Button.hpp"
  18. #include "GUICINCLUDE:GUIC_PathString.hpp"
  19. #include "GUICINCLUDE:GUIC_Label.hpp"
  20. #include "GUICINCLUDE:GUIC_Number.hpp"
  21. #include "GUICINCLUDE:GUIC_Text.hpp"
  22. #include "GUICINCLUDE:GUIC_Checkbox.hpp"
  23. #include "GUICINCLUDE:GUIC_Exceptions.hpp"
  24. #include "GUICINCLUDE:GUIC_Error.hpp"
  25. #include "GUICINCLUDE:GUIC_Message.hpp"
  26. #include "GUICINCLUDE:GUIC_File.hpp"
  27. #include "GUICINCLUDE:GUIC_GGFXPicture.hpp"
  28.  
  29. #include "ManagerWindow.hpp"
  30. #include "ThumbnailWindow.hpp"
  31. #include "PictureWindow.hpp"
  32. #include "GalleryWindow.hpp"
  33. #include "InformationWindow.hpp"
  34.  
  35. /*********************************************************************************************************/
  36.  
  37. static class ListEntryC : public GUIC_ObjectC
  38.     {
  39.     public:
  40.         ListEntryC                (STRPTR s);
  41.         ~ListEntryC                (VOID);
  42.         LONG        compare    (GUIC_ObjectC &o);
  43.         STRPTR     str;
  44.     protected:
  45.         VOID         cleanUp    (VOID);
  46.     };
  47.     
  48. ListEntryC::ListEntryC                (STRPTR s)
  49. {
  50.     str=0;
  51.     GUIC_SystemC::reallocString(&str, s);
  52. }
  53. ListEntryC::~ListEntryC            (VOID)
  54. {
  55.     cleanUp();
  56. }
  57. VOID    ListEntryC::cleanUp        (VOID)
  58. {
  59.     if (str) delete str; str=0;
  60. }
  61. LONG    ListEntryC::compare    (GUIC_ObjectC &o)
  62. {
  63.     ListEntryC *e = (ListEntryC *)&o; 
  64.     return Stricmp(str, e->str); 
  65. }
  66.  
  67.  
  68. /*********************************************************************************************************/
  69.  
  70. ManagerWindowC::ManagerWindowC                         (GUIC_ApplicationC &a, GUIC_ScreenC &s) : GUIC_WindowC (-1,1000, 62, 36)
  71. {
  72.     app         = &a;
  73.     screen    = &s;
  74.     tWindow    = 0;
  75.     pWindow    = 0;
  76.     iWindow    = 0;
  77.     tWidth        = 200;
  78.     tHeight    = 200;
  79.     
  80.     sourceLabel        = new GUIC_LabelC             ( 1, 1,25, 2, "_Source _Files");
  81.     sourcePath            = new GUIC_PathStringC    ( 1, 3,25, 2, "RAM:");
  82.     source                = new GUIC_ListviewC        ( 1, 5,25,20);
  83.     
  84.     deleteFile            = new GUIC_ButtonC            (27, 7, 8, 2, "_Delete");
  85.     moveFile                = new GUIC_ButtonC            (27,10, 8, 2, "_Move ->");
  86.     copyFile                = new GUIC_ButtonC            (27,13, 8, 2, "_Copy ->");
  87.     genThumbnail        = new GUIC_ButtonC            (27,16, 8, 2, "_Thumb"); 
  88.  
  89.     destinationLabel    = new GUIC_LabelC             (36, 1,25, 2, "D_estination d_irectories");
  90.     destinationPath    = new GUIC_PathStringC    (36, 3,25, 2, "RAM:");
  91.     destination            = new GUIC_ListviewC        (36, 5,25,20);
  92.     
  93.     width                    = new GUIC_NumberC        ( 1,26,15, 2, 0);
  94.     mal                        = new GUIC_TextC                (16,26, 5, 2, "x");
  95.     height                    = new GUIC_NumberC        (21,26,15, 2, 0);
  96.     pixel                    = new GUIC_TextC                (36,26, 8, 2, "Pixel");
  97.     
  98.     showLabel            = new GUIC_LabelC            (50,26, 8, 2, "Sho_w:");
  99.     show                    = new GUIC_CheckboxC        (58,26, 3, 2, TRUE);
  100.     
  101.     scaleLabel            = new GUIC_LabelC            (50,28, 8, 2, "Sc_ale:");
  102.     scale                    = new GUIC_CheckboxC        (58,28, 3, 2, TRUE);
  103.     
  104.     centerLabel        = new GUIC_LabelC            (50,30, 8, 2, "Ce_nter:");
  105.     center                    = new GUIC_CheckboxC        (58,30, 3, 2, TRUE);
  106.     
  107.     message                = new GUIC_TextC                ( 1,33,60, 2, "");
  108.     
  109.     sourcePath            -> setHelp ("Sets the source path of pictures.");
  110.     source                -> setHelp ("Select the picture to work with.");
  111.     destinationPath    -> setHelp ("Sets the destination path for pictures.");
  112.     destination            -> setHelp ("Select the directory to copy the picture.");
  113.     deleteFile            -> setHelp ("Deletes the picture from disk.");
  114.     moveFile                -> setHelp ("Moves the picture to the selected directory.");
  115.     copyFile                -> setHelp ("Copies the picture to the selected directory.");
  116.     genThumbnail        -> setHelp ("Generates a thumbnail for the picture.");
  117.     width                    -> setHelp ("The width of the picture (when shown).");
  118.     height                    -> setHelp ("The height of the picture (when shown).");
  119.     show                    -> setHelp ("Check if you want to see the picture.");
  120.     scale                    -> setHelp ("Scale the picture if it is too large.");
  121.     center                    -> setHelp ("Center the picture on the screen.");
  122.     
  123.     mal         -> setBorder(FALSE);
  124.     pixel        -> setBorder(FALSE);
  125.     
  126.     width        -> setFrontPen ( GUIC_ShinePen );
  127.     height        -> setFrontPen ( GUIC_ShinePen );
  128.     message    -> setFrontPen ( GUIC_FillPen );
  129.     
  130.     mal     -> setJustification ( GUIC_Center );
  131.     width    -> setJustification ( GUIC_Center );
  132.     height    -> setJustification ( GUIC_Center );
  133.     pixel    -> setJustification ( GUIC_Center );
  134.     
  135.     source                -> setShortcut ('f');
  136.     sourcePath            -> setShortcut ('s');
  137.     destination            -> setShortcut ('i');
  138.     destinationPath    -> setShortcut ('e');
  139.     show                    -> setShortcut ('w');
  140.     scale                    -> setShortcut ('a');
  141.     center                    -> setShortcut ('n');
  142.     
  143.     add ( sourceLabel );
  144.     add ( destinationLabel );
  145.     add ( sourcePath );
  146.     add ( destinationPath );
  147.     add ( source );
  148.     add ( destination );
  149.     add ( deleteFile );
  150.     add ( moveFile );
  151.     add ( copyFile );
  152.     add ( genThumbnail );
  153.     add ( width );
  154.     add ( mal );
  155.     add ( height );
  156.     add ( pixel );
  157.     add ( showLabel );
  158.     add ( show );
  159.     add ( scaleLabel );
  160.     add ( scale );
  161.     add ( centerLabel );
  162.     add ( center );
  163.     add ( message );
  164.     
  165.     app->addPrefs("ManagerWindow", this);
  166.     app->addPrefs("Source",                 sourcePath);
  167.     app->addPrefs("Destination",        destinationPath);
  168.     app->addPrefs("ShowPicture",        show);
  169.     app->addPrefs("ScalePicture",        scale);
  170.     app->addPrefs("CenterPicture",     center);
  171.     
  172.     setGuideContext("ManagerWindow");
  173.     setTitle ( "Gallery Picture Manager");
  174.     setHelp ( TRUE );
  175.     
  176.     activate();
  177. }
  178. ManagerWindowC::~ManagerWindowC                     (VOID)
  179. {
  180.     cleanUp();
  181. }
  182.  
  183. /*********************************************************************************************************/
  184.  
  185. VOID        ManagerWindowC::setWindows                (ThumbnailWindowC &t, PictureWindowC &p)
  186. {
  187.     tWindow = &t;
  188.     pWindow = &p;
  189.     
  190.     pWindow->setTextGadget ( message );
  191.     pWindow->setNumberGadgets ( width, height );
  192.     
  193.     tWindow->setTextGadget ( message );
  194. }
  195. VOID        ManagerWindowC::setThumbnailDim        (LONG x, LONG y)
  196. {
  197.     if (! tWindow) return;
  198.     tWindow->setDimensions(x,y);
  199.     tWidth = x;
  200.     tHeight = y;
  201. }
  202.  
  203. VOID        ManagerWindowC::scanSource                (STRPTR s)
  204. {
  205.     message->set("Scanning source directory for pictures.");
  206.     
  207.     source->delAllItems();
  208.     
  209.     try
  210.         {
  211.         GUIC_FileExamineC *filex = 0;
  212.         GUIC_DirectoryExamineC direx (s);
  213.         
  214.         while ((filex = direx.examineNext() ))
  215.             {
  216.             STRING fileName = filex->getFilePart();
  217.             if (filex->getFileType() == GUIC_PictureFile) 
  218.                 {
  219.                 if (fileName.right(5) == (STRING)"_.jpg") continue;
  220.                 source->addItem(fileName);
  221.                 }
  222.             }
  223.         }
  224.     catch (GUIC_Exception &e)
  225.         {
  226.         GUIC_ErrorC err("Exception caught:", e.getMessage() );
  227.         err.request(this);
  228.         }
  229.     
  230.     source->sortItems();
  231. }
  232. VOID        ManagerWindowC::scanDestination            (STRPTR userRoot)
  233. {
  234.     message->set("Scanning destination directory.");
  235.     
  236.     destination->delAllItems();
  237.     
  238.     try
  239.         {
  240.         GUIC_FileExamineC *filex = 0;
  241.         GUIC_DirectoryExamineC direx (userRoot);
  242.         
  243.         while ((filex = direx.examineNext() ))
  244.             {
  245.             if (filex->isDirectory()) destination->addItem( filex->getFilePart() );
  246.             }
  247.         }
  248.     catch (GUIC_Exception &e)
  249.         {
  250.         GUIC_ErrorC err("Exception caught:", e.getMessage() );
  251.         err.request(this);
  252.         }
  253.     
  254.     destination->sortItems();
  255. }
  256.  
  257. STRPTR    ManagerWindowC::getDestination            (LONG i)
  258. {
  259.     return destination->getItem(i);
  260. }
  261.  
  262. STRPTR    ManagerWindowC::getThumbnailName    (VOID)
  263. {
  264.     ULONG sel = source->getSelected();
  265.     if (sel == -1) return "";
  266.     
  267.     STRING fileName = source->getItem(sel);
  268.  
  269.     LONG i = fileName.length();
  270.     while (--i) if (fileName[i] == '.') break;
  271.  
  272.     STRING thumbName = sourcePath->get();
  273.     if (thumbName.right(1) != (STRING) ":") thumbName += "/";
  274.     thumbName += fileName.left(i);
  275.     thumbName += "_.jpg";
  276.  
  277.     static STRPTR result = 0;
  278.     GUIC_SystemC::reallocString ( &result, thumbName);
  279.     
  280.     return result;
  281. }
  282. STRPTR    ManagerWindowC::getPictureName            (VOID)
  283. {
  284.     ULONG sel = source->getSelected();
  285.     if (sel == -1) return "";
  286.     
  287.     STRING fileName = source->getItem(sel);
  288.  
  289.     STRING picName = sourcePath->get();
  290.     if (picName.right(1) != (STRING) ":") picName += "/";
  291.     picName += fileName;
  292.  
  293.     static STRPTR result = 0;
  294.     GUIC_SystemC::reallocString ( &result, picName);
  295.     
  296.     return result;
  297. }
  298.  
  299. VOID        ManagerWindowC::showPic                    (VOID)
  300. {
  301.     sleep();
  302.     
  303.     tWindow->showThumbnail ( getThumbnailName() );
  304.     if ( show->get() ) pWindow->showPicture ( getPictureName() );
  305.     
  306.     waken();
  307. }
  308. VOID        ManagerWindowC::deletePic                    (VOID)
  309. {
  310.     GUIC_FileC f1 ( getPictureName() );
  311.     GUIC_FileC f2 ( getThumbnailName() ); 
  312.     
  313.     if (f1.remove()) 
  314.         {
  315.         source->delItem();
  316.         showPic();
  317.         }
  318.     else 
  319.         {
  320.         GUIC_ErrorC err ("Caution:", "Could not delete file from disk.");
  321.         err.request(this);
  322.         }
  323.                 
  324.     if (! f2.remove())
  325.         {
  326.         GUIC_ErrorC err ("Caution:", "Could not delete thumbnail from disk.");
  327.         err.request(this);
  328.         }
  329. }
  330. VOID        ManagerWindowC::movePic                    (VOID)
  331. {
  332.     if (copyPic()) deletePic();
  333. }
  334. BOOL        ManagerWindowC::copyPic                        (VOID)
  335. {
  336.     GUIC_FileC f1    ( getPictureName() );
  337.     GUIC_FileC f2    ( getThumbnailName() ); 
  338.     
  339.     STRPTR path1    = destinationPath->get();
  340.     STRPTR path2    = getDestination( destination->getSelected() );
  341.     STRPTR file        = f1.getFilePart();
  342.     STRPTR thumb    = f2.getFilePart();
  343.  
  344.     LONG lenPic         = strlen (path1) + strlen(path2) + strlen(file) + 5;
  345.     LONG lenThumb    = strlen (path1) + strlen(path2) + strlen(thumb) + 5;
  346.     
  347.     STRPTR buffer1    = new CHAR [lenPic];
  348.     STRPTR buffer2    = new CHAR [lenThumb];
  349.     
  350.     strcpy(buffer1, path1);
  351.     strcpy(buffer2, path1);
  352.     
  353.     AddPart(buffer1, path2, lenPic);
  354.     AddPart(buffer2, path2, lenThumb);
  355.  
  356.     AddPart(buffer1, file, lenPic);
  357.     AddPart(buffer2, thumb, lenThumb);
  358.     
  359.     
  360.     if (f1.doesExist())
  361.         {
  362.         GUIC_FileC *dest = new GUIC_FileC (buffer1);
  363.         BOOL exists=dest->doesExist();
  364.         delete dest;
  365.         
  366.         if (exists)
  367.             {
  368.             STRING s="File\n'";
  369.             s += buffer1;
  370.             s += "'\nexists already";
  371.             GUIC_MessageC msg ("Caution:", s, "Override|More information|Cancel");
  372.             LONG result = msg.request(this);
  373.             if (result == 0) return FALSE;
  374.             if (result == 2) 
  375.                 {
  376.                 sleep();
  377.                 pWindow->showPicture("");
  378.                 if (iWindow == 0) iWindow = new InformationWindowC(*app, *screen);
  379.                 screen->add(iWindow);
  380.                 iWindow->setPictures(f1.getName(), buffer1);
  381.                 waken();
  382.                 return FALSE;
  383.                 }
  384.             }
  385.             
  386.         if ( f1.copy ( buffer1 ) )
  387.             {
  388.             if ( f2.doesExist() )
  389.                 {
  390.                 if ( f2.copy ( buffer2 ) )
  391.                     {
  392.                     return TRUE;
  393.                     }
  394.                 else
  395.                     {
  396.                     GUIC_ErrorC err ("Caution:", "Could not copy thumbnail to destination.");
  397.                     err.request(this);
  398.                     }
  399.                 }
  400.             else 
  401.                 {
  402.                 GUIC_FileC oldThumbnail (buffer2);
  403.                 oldThumbnail.remove();
  404.                 return TRUE;
  405.                 }
  406.             }
  407.         else
  408.             {
  409.             GUIC_ErrorC err ("Caution:", "Could not copy picture to destination.");
  410.             err.request(this);
  411.             }
  412.         }
  413.         
  414.     return FALSE;
  415. }
  416.  
  417. BOOL        ManagerWindowC::action                        (GUIC_EventC &e)
  418. {
  419.     switch (e.id)
  420.         {
  421.         case GUIC_GadgetEvent:
  422.             if (e.gadget == (GUIC_GadgetC*) sourcePath)
  423.                 {
  424.                 sleep();
  425.                 scanSource( sourcePath->get() );
  426.                 showPic();
  427.                 if (source->length() > 0) enableGadgets(); else disableGadgets();
  428.                 waken();
  429.                 }
  430.             else if (e.gadget == (GUIC_GadgetC*) destinationPath)
  431.                 {
  432.                 sleep();
  433.                 scanDestination( destinationPath->get() );
  434.                 waken();
  435.                 }
  436.             else if (e.gadget == (GUIC_GadgetC*) source)
  437.                 {
  438.                 showPic();
  439.                 }
  440.             else if (e.gadget == (GUIC_GadgetC*) show)
  441.                 {
  442.                 if ( show->get() ) 
  443.                     {
  444.                     sleep();
  445.                     pWindow->showPicture ( getPictureName() );
  446.                     waken();
  447.                     scale->enable();
  448.                     center->enable();
  449.                     genThumbnail->enable();
  450.                     }
  451.                 else 
  452.                     {
  453.                     scale->disable();
  454.                     center->disable();
  455.                     genThumbnail->disable();
  456.                     }
  457.                 }
  458.             else if (e.gadget == (GUIC_GadgetC*) scale)
  459.                 {
  460.                 pWindow->setScaled(scale->get());
  461.                 }
  462.             else if (e.gadget == (GUIC_GadgetC*) center)
  463.                 {
  464.                 pWindow->setCentered(center->get());
  465.                 }
  466.             else if (e.gadget == (GUIC_GadgetC*) deleteFile)
  467.                 {
  468.                 sleep();
  469.                 deletePic();
  470.                 if (source->length() == 0) disableGadgets();
  471.                 waken();
  472.                 }
  473.             else if (e.gadget == (GUIC_GadgetC*) moveFile)
  474.                 {
  475.                 sleep();
  476.                 movePic();
  477.                 if (source->length() == 0) disableGadgets();
  478.                 waken();
  479.                 }
  480.             else if (e.gadget == (GUIC_GadgetC*) copyFile)
  481.                 {
  482.                 sleep();
  483.                 copyPic();
  484.                 waken();
  485.                 }
  486.             else if (e.gadget == (GUIC_GadgetC*) genThumbnail)
  487.                 {
  488.                 sleep();
  489.                 
  490.                 try
  491.                     {
  492.                     GUIC_GGFXPictureC *pic = pWindow->getPicture();
  493.                     if (! pic) return TRUE;
  494.                     pic->scaleToBox    (tWidth, tHeight);
  495.                     pic->saveJPEG     (getThumbnailName(), 90);
  496.                     tWindow->showThumbnail ( getThumbnailName() );
  497.                     }
  498.                 catch (GUIC_Exception &e) { GUIC_ErrorC err ("Exception caught:", e.getMessage()); err.request(this); }
  499.                 
  500.                 waken();
  501.                 }
  502.             return TRUE;
  503.             break;
  504.         case GUIC_OpenWindow:
  505.         
  506.             if (! show->get() )
  507.                 {
  508.                 scale->disable();
  509.                 center->disable();
  510.                 genThumbnail->disable();
  511.                 }
  512.                 
  513.             sleep();
  514.             
  515.             scanSource( sourcePath->get() );
  516.             if (source->length() == 0) disableGadgets();
  517.             
  518.             scanDestination( destinationPath->get() );
  519.             
  520.             tWindow->showThumbnail ( getThumbnailName() );
  521.             
  522.             pWindow->setScaled(scale->get());
  523.             pWindow->setCentered(center->get());
  524.             if (show->get() ) pWindow->showPicture ( getPictureName() );
  525.  
  526.             waken();
  527.             
  528.             return TRUE;
  529.             break;
  530.         case GUIC_CloseWindow:
  531.             return FALSE;
  532.             break;
  533.         default:
  534.             cerr << "Got an event: " << e.id << endl;
  535.         }
  536.     
  537.     return FALSE;
  538. }
  539.  
  540. VOID        ManagerWindowC::disableGadgets            (VOID)
  541. {
  542.     deleteFile->disable();
  543.     moveFile->disable();
  544.     copyFile->disable();
  545.     genThumbnail->disable();
  546. }
  547. VOID        ManagerWindowC::enableGadgets            (VOID)
  548. {
  549.     deleteFile->enable();
  550.     moveFile->enable();
  551.     copyFile->enable();
  552.     if (show->get() ) genThumbnail->enable();
  553. }
  554.  
  555. STRPTR    ManagerWindowC::getClass                    (VOID)
  556. {
  557.     return "ManagerWindowC";
  558. }
  559.  
  560. /*********************************************************************************************************/
  561.  
  562. VOID         ManagerWindowC::cleanUp                        (VOID)
  563. {    
  564.     if (iWindow) delete iWindow;
  565.     
  566.     delete sourcePath;
  567.     delete sourceLabel;
  568.     delete source;
  569.     
  570.     delete destinationPath;
  571.     delete destinationLabel;
  572.     delete destination;
  573.  
  574.     delete deleteFile;
  575.     delete moveFile;
  576.     delete copyFile;
  577.     delete genThumbnail;
  578.     
  579.     delete width;
  580.     delete mal;
  581.     delete height;
  582.     delete pixel;
  583.     delete showLabel;
  584.     delete show;
  585.     delete scaleLabel;
  586.     delete scale;
  587.     delete centerLabel;
  588.     delete center;
  589.     delete message;
  590. }
  591.  
  592.